package com.pda.common_easy.boardcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

public class CustomReceiver extends BroadcastReceiver {

    static public class Dataa {
        String str="";
        int type=0;

        public Dataa(String str, int type) {
            this.str = str;
            this.type = type;
        }

        public String getStr() {
            return str;
        }

        public void setStr(String str) {
            this.str = str;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }
    }

    private String tag = "CustomReceiver";

    public interface PListener1 {
        void setData(Dataa dataa);
    }

    public interface PListener2 {
        void setText(String string);
    }

    private PListener1 listener1 = null;
    private PListener2 listener2 = null;

    public CustomReceiver(PListener1 listener1, PListener2 listener2) {
        super();
        this.listener1 = listener1;
        this.listener2 = listener2;
    }

    private String getConnectionType(int type) {
        String connType = "";
        if (type == ConnectivityManager.TYPE_MOBILE) {
            connType = "4G网络数据";
        } else if (type == ConnectivityManager.TYPE_WIFI) {
            connType = "WIFI网络";
        }
        return connType;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
//        if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
//            int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
//            switch (wifiState) {
//                case WifiManager.WIFI_STATE_DISABLED:
//                    break;
//                case WifiManager.WIFI_STATE_DISABLING:
//                    break;
//            }
//        }
        // 监听网络连接，包括wifi和移动数据的打开和关闭,以及连接上可用的连接都会接到监听
        if (listener1 != null && ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
            NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            if (info != null) {
                //如果当前的网络连接成功并且网络连接可用
                Intent intent1 = new Intent();
                if (NetworkInfo.State.CONNECTED == info.getState() && info.isAvailable()) {
                    if (info.getType() == ConnectivityManager.TYPE_WIFI || info.getType() == ConnectivityManager.TYPE_MOBILE) {
                        listener1.setData( new Dataa("网络状态:正常",0) );
                    }
                } else {
                    listener1.setData(new Dataa("网络状态:异常",-1) );
                }
            }
        } else if (listener2 != null && intent.getAction().equals(ScannerInterface.RES_ACTION)) {
            final String scanResult = intent.getStringExtra("value");
            if (scanResult.length() > 0) {
                listener2.setText(scanResult);
            }
            else {
                Toast.makeText(context, "解码失败！", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
